<?php
//	  PHPcongrats - Holiday and Birthday Mailer
//	
//    Copyright (C) 2011  Matthias Nass
//
//    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.


function saltTheHash($givenPassword, $givenSalt=null)
{
	$saltHash = array();
	$saltHash['salt'] = $givenSalt;
	$saltHash['password'] = $givenPassword;
	$saltHash['saltedHash'] = null;

	$saltHash['privateKey'] = "
  WKobxlp9Gr1
  ASDWVCC,@i*6I714N7@744GIbYbkUbasdeU4uv/E0Y(h:F;Fao*OReDK?LP66ajhDvb-3IF6FM%zEk*8ZYf>@30@s
  H-Gs%s4DAiO##1*4s02SSDFEiWUq*rO>8&5V:Ut-&Wa)v,&qVS1MVNy-IwH@Y6-Gs%s43&k6CVIELQCY5cM30XW97LY
  1j0l2OU<<9c5uYGHk:
  e3lNTe4R4LNT*f!9VoOFfO3<6CM8kfVAM<5%6Vve86+gKm*m(Bnce?E3AiWRLRZjV5yvZT98P/1!g!ZtGs%s4?;h5%
  ";

	if($saltHash['salt'] == null)
	{
		$saltHash['salt'] = microtime();
		$saltHash['salt'] = crc32($saltHash['salt']);
		$saltHash['salt'] = pack('N', $saltHash['salt']);
		$saltHash['salt'] = base64_encode($saltHash['salt']);
		$saltHash['salt'] = substr($saltHash['salt'], 0, 6);
	}
	else if(strpos($saltHash['salt'], "|") !== false)
	{
		$saltHash['salt'] = substr($saltHash['salt'], 0, strpos($saltHash['salt'], "|"));
	}

	$saltHash['saltedHash'] = $saltHash['salt'];
	$saltHash['saltedHash'].= "|";
	$saltHash['saltedHash'].= sha1($saltHash['privateKey']);
	$saltHash['saltedHash'].= sha1($saltHash['salt'].$saltHash['password']);

	return $saltHash['saltedHash'];
}
?>